package tools

import (
	"context"
	"github.com/gocolly/colly"
	"gorm.io/gorm/logger"
	"net/http"
	"time"
)

// 请求超时时间 90S
var reqTimeOut = 90 * time.Second
var count uint64 = 1

var mLogger logger.Interface
var mCtx context.Context
var mHeader *http.Header

func init() {

}

// 设置请求头
func init() {
	// 获取日志记录对象
	mLogger, mCtx = GetLogger()

	header := http.Header{}

	header.Set("Accept", "text/html,application/xhtml+xml,application/xml")
	header.Set("Accept-Encoding", "gzip, deflate")
	header.Set("Accept-Language", "zh-CN,zh")
	header.Set("Cache-Control", "no-cache")
	header.Set("Host", "www.stats.gov.cn")
	header.Set("Pragma", "no-cache")
	header.Set("Referer", "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/")
	header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102")
}

// 创建请求对象
func GetCollector() *colly.Collector {
	collector := colly.NewCollector()

	// collector.SetDebugger(&debug.LogDebugger{})
	collector.SetRequestTimeout(reqTimeOut)
	collector.OnRequest(func(req *colly.Request) {
		req.Headers = mHeader
		mLogger.Info(mCtx, "第%d次请求：%s", count, req.URL)

		count++
	})

	return collector
}
